<script lang="ts" setup name="room-action">
import { OrderType } from '@/enums'
import { uploadImage } from '@/services/consult';
import { showToast } from 'vant';
import type { UploaderAfterRead } from 'vant/lib/uploader/types';
import { ref } from 'vue'
defineProps<{ status: OrderType}>()
const emit = defineEmits<{
  (e: 'send-text', text: string):void
  (e: 'send-img', pic: { id: string; url: string }): void
}>()
// 发送消息
const text = ref('')
const sendText = () => {
  // 判断输入框中的内容是否为空
  if(text.value.trim() === '') return showToast('内容不能为空')
  console.log('sendText', text.value)
  // 将数据提交至问诊室页面中
  emit('send-text', text.value)
  // 清空输入框
  text.value = ''
}
// 图片聊天
const afterRead: UploaderAfterRead = async(item) => {
  // 判断是否数组
  if(Array.isArray(item)) return
  if(item.file) {
    // 将选中的图片上传至服务器
    const res = await uploadImage(item.file)
    // 获取图片地址
    console.log(res.data.url)
    // 将图片地址赋值给 Room 页面
    emit('send-img', res.data)
  }
}
</script>

<template>
  <div class="room-action">
    <van-field type="text" class="input" v-model="text" :border="false" :disabled="status !== OrderType.ConsultChat" placeholder="问医生" autocomplete="off" @keyup.enter="sendText" />
    <van-uploader :after-read="afterRead" :preview-image="false">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>